package com.lskj.shiro.controller;

import com.lskj.shiro.entity.SysUser;
import com.lskj.shiro.service.ISysUserService;
import com.lskj.shiro.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author zzq
 * @Date 2020/4/3 15:42
 * @Version 1.0
 */
@RequestMapping("users")
@RestController
public class UserController {

    @Autowired
    private ISysUserService sysUserService;

    @GetMapping()
    public ResultVO<SysUser> getAll() {
        ResultVO resultVO = ResultVO.ok();
        resultVO.setData(sysUserService.list());
        return resultVO;
    }

    @GetMapping("{id}")
    public ResultVO<SysUser> getUserById(@PathVariable String id) {
        SysUser sysUser = sysUserService.getById(id);
        ResultVO resultVO = ResultVO.ok();
        resultVO.setData(sysUser);
        return resultVO;
    }

    @PutMapping("{id}")
    public ResultVO<SysUser> updateUserById(@RequestBody SysUser sysUser) {
        boolean flag = sysUserService.updateById(sysUser);
        ResultVO resultVO = ResultVO.ok();
        resultVO.setData(flag);
        return resultVO;
    }

    @DeleteMapping("{id}")
    public ResultVO<SysUser> deleteUserById(@PathVariable String id) {
        boolean flag = sysUserService.removeById(id);
        ResultVO resultVO = ResultVO.ok();
        resultVO.setData(flag);
        return resultVO;
    }

    @PostMapping
    public ResultVO<SysUser> addUserById(@RequestBody SysUser sysUser) {
        boolean flag = sysUserService.save(sysUser);
        ResultVO resultVO = ResultVO.ok();
        resultVO.setData(flag);
        return resultVO;
    }
}
